using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.AnalysisServices.AdomdClient;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess
{
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;
    using Galaktika.BI.Extensibility.Dom.Dac;

    internal static class OlapDataHelper
    {
        /*
        public static OlapDataView CreateView(IBindingContext context, string connectionString, string query)
        {
            OlapDataView view = null;
            IExecutionContext exe = (IExecutionContext)context.Services.GetService(typeof(IExecutionContext));
            if (exe != null)
            {
                AdomdConnection conn = AdomdConnectionPool.GetConnection(exe.Parse(context, connectionString));
                //using (AdomdConnection conn = new AdomdConnection())
                {
                    //conn.Open();
                    using (AdomdCommand cmd = new AdomdCommand(exe.Parse(context, query), conn))
                    {
                        CellSet cs = cmd.ExecuteCellSet();
                        //cs.OlapInfo.CubeInfo.Cubes[0].LastSchemaUpdate

                        view = OlapDataView.Create(conn.ConnectionString, cs);
                    }
                    //conn.Close();
                }
            }

            return view;
        }
        */

        public static OlapDataView CreateView(IBindingContext context, BrickConnection brickConn, string query)
        {
            OlapDataView view = null;
            IExecutionContext exe = (IExecutionContext)context.Services.GetService(typeof(IExecutionContext));
            if (exe != null)
            {
                AdomdConnection conn = AdomdConnectionPool.GetConnection(exe.Parse(context, brickConn.ConnectionString));
                //using (AdomdConnection conn = new AdomdConnection())
                {
                    //conn.Open();
                    using (AdomdCommand cmd = new AdomdCommand(exe.Parse(context, query), conn))
                    {
                        CellSet cs = cmd.ExecuteCellSet();

                        brickConn.UserData[BrickConnection.CONNECT_LAST_DATA_UPDATE] =
                                cs.OlapInfo.CubeInfo.Cubes[0].LastDataUpdate;

                        brickConn.UserData[BrickConnection.CONNECT_LAST_SCHEMA_UPDATE] = 
                                cs.OlapInfo.CubeInfo.Cubes[0].LastSchemaUpdate;

                        view = OlapDataView.Create(conn.ConnectionString, cmd.CommandText, cs);
                    }
                    //conn.Close();
                }
            }

            return view;
        }

        /*
        public static OlapDataView CreateView(IBindingContext context, string connectionString, string query)
        {
            OlapDataView view = null;
            IExecutionContext exe = (IExecutionContext)context.Services.GetService(typeof(IExecutionContext));
            if (exe != null)
            {
                string parsedConnectionString = exe.Parse(context, connectionString);
                string parsedQuery = exe.Parse(context, query);
                view = GetView(parsedConnectionString, parsedQuery);
            }

            return view;
        }

        private static OlapDataView GetView(string connectionString, string query)
        {
            if (OlapConnectionPool.IsInitialized)
            {
                CellSet cs = OlapConnectionPool.ExecutePooledQuery(connectionString, query);
                return OlapDataView.Create(cs);
            }
            else
            {
                using (AdomdConnection conn = new AdomdConnection(connectionString))
                {
                    OlapDataView view = null;
                    conn.Open();
                    using (AdomdCommand cmd = new AdomdCommand(query, conn))
                    {
                        CellSet cs = cmd.ExecuteCellSet();
                        view = OlapDataView.Create(cs);
                    }
                    conn.Close();

                    return view;
                }
            }
        }
        */
    }
}
